<?php

namespace App\Services;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Auth;
use common\utils\SysCode;
use App\Models\DemoUser;

class DemoService
{
    /**
     * 注册试玩账户
     */
    public static function register($params)
    {
        if (self::isExistName($params['username'])) {
            error_exit(SysCode::DEMO_USERNAME_EXIST);
        }
        $password = $params['password'];
        $params['register_ip'] = Request::getClientIp();
        $params['salt'] = salt();
        $params['password'] = password_encrypt($params['password'], $params['salt']);
        DemoUser::insert($params);
        return UserService::login(['demo' => $params['username'], 'password' => $password]);
    }

    /**
     * 检验试玩账户名是否存在
     */
    public static function isExistName($name)
    {
        $demoUser = DemoUser::where('username', $name)->first();
        if ($demoUser) {
            return true;
        } else {
            return false;
        }
    }
}
